

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.distributions &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.distributions</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.distributions 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">import</span> <span class="nn">decimal</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Sequence</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>  <span class="c1"># NOQA</span>

    <span class="n">CategoricalChoiceType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">BaseDistribution</span><span class="p">(</span><span class="nb">object</span><span class="p">,</span> <span class="n">metaclass</span><span class="o">=</span><span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for distributions.</span>

<span class="sd">    Note that distribution classes are not supposed to be called by library users.</span>
<span class="sd">    They are used by :class:`~optuna.trial.Trial` and :class:`~optuna.samplers` internally.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">to_external_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; Any</span>
        <span class="sd">&quot;&quot;&quot;Convert internal representation of a parameter value into external representation.</span>

<span class="sd">        Args:</span>
<span class="sd">            param_value_in_internal_repr:</span>
<span class="sd">                Optuna&#39;s internal representation of a parameter value.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Optuna&#39;s external representation of a parameter value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">param_value_in_internal_repr</span>

    <span class="k">def</span> <span class="nf">to_internal_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_external_repr</span><span class="p">):</span>
        <span class="c1"># type: (Any) -&gt; float</span>
        <span class="sd">&quot;&quot;&quot;Convert external representation of a parameter value into internal representation.</span>

<span class="sd">        Args:</span>
<span class="sd">            param_value_in_external_repr:</span>
<span class="sd">                Optuna&#39;s external representation of a parameter value.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Optuna&#39;s internal representation of a parameter value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">param_value_in_external_repr</span>

    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>
        <span class="sd">&quot;&quot;&quot;Test whether the range of this distribution contains just a single value.</span>

<span class="sd">        When this method returns :obj:`True`, :mod:`~optuna.samplers` always sample</span>
<span class="sd">        the same value from the distribution.</span>

<span class="sd">        Returns:</span>
<span class="sd">            :obj:`True` if the range of this distribution contains just a single value,</span>
<span class="sd">            otherwise :obj:`False`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span>

    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>
        <span class="sd">&quot;&quot;&quot;Test if a parameter value is contained in the range of this distribution.</span>

<span class="sd">        Args:</span>
<span class="sd">            param_value_in_internal_repr:</span>
<span class="sd">                Optuna&#39;s internal representation of a parameter value.</span>

<span class="sd">        Returns:</span>
<span class="sd">            :obj:`True` if the parameter value is contained in the range of this distribution,</span>
<span class="sd">            otherwise :obj:`False`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span>

    <span class="k">def</span> <span class="nf">_asdict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="c1"># type: (Any) -&gt; bool</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">BaseDistribution</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">type</span><span class="p">(</span><span class="n">other</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="vm">__dict__</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; int</span>

        <span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,)</span> <span class="o">+</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">())))</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>

        <span class="n">kwargs</span> <span class="o">=</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">=</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">()))</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">(</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>


<div class="viewcode-block" id="UniformDistribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.UniformDistribution">[文档]</a><span class="k">class</span> <span class="nc">UniformDistribution</span><span class="p">(</span><span class="n">BaseDistribution</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A uniform distribution in the linear domain.</span>

<span class="sd">    This object is instantiated by :func:`~optuna.trial.Trial.suggest_uniform`, and passed to</span>
<span class="sd">    :mod:`~optuna.samplers` in general.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        low:</span>
<span class="sd">            Lower endpoint of the range of the distribution. ``low`` is included in the range.</span>
<span class="sd">        high:</span>
<span class="sd">            Upper endpoint of the range of the distribution. ``high`` is excluded from the range.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">):</span>
        <span class="c1"># type: (float, float) -&gt; None</span>

        <span class="k">if</span> <span class="n">low</span> <span class="o">&gt;</span> <span class="n">high</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be smaller than or equal to the `high` value &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>

<div class="viewcode-block" id="UniformDistribution.single"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.UniformDistribution.single">[文档]</a>    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>

    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>

        <span class="n">value</span> <span class="o">=</span> <span class="n">param_value_in_internal_repr</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">value</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">value</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>


<div class="viewcode-block" id="LogUniformDistribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.LogUniformDistribution">[文档]</a><span class="k">class</span> <span class="nc">LogUniformDistribution</span><span class="p">(</span><span class="n">BaseDistribution</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A uniform distribution in the log domain.</span>

<span class="sd">    This object is instantiated by :func:`~optuna.trial.Trial.suggest_loguniform`, and passed to</span>
<span class="sd">    :mod:`~optuna.samplers` in general.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        low:</span>
<span class="sd">            Lower endpoint of the range of the distribution. ``low`` is included in the range.</span>
<span class="sd">        high:</span>
<span class="sd">            Upper endpoint of the range of the distribution. ``high`` is excluded from the range.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">):</span>
        <span class="c1"># type: (float, float) -&gt; None</span>

        <span class="k">if</span> <span class="n">low</span> <span class="o">&gt;</span> <span class="n">high</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be smaller than or equal to the `high` value &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">low</span> <span class="o">&lt;=</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be larger than 0 for a log distribution &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>

<div class="viewcode-block" id="LogUniformDistribution.single"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.LogUniformDistribution.single">[文档]</a>    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>

    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>

        <span class="n">value</span> <span class="o">=</span> <span class="n">param_value_in_internal_repr</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">value</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">value</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>


<div class="viewcode-block" id="DiscreteUniformDistribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.DiscreteUniformDistribution">[文档]</a><span class="k">class</span> <span class="nc">DiscreteUniformDistribution</span><span class="p">(</span><span class="n">BaseDistribution</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A discretized uniform distribution in the linear domain.</span>

<span class="sd">    This object is instantiated by :func:`~optuna.trial.Trial.suggest_discrete_uniform`, and passed</span>
<span class="sd">    to :mod:`~optuna.samplers` in general.</span>

<span class="sd">    .. note::</span>
<span class="sd">        If the range :math:`[\\mathsf{low}, \\mathsf{high}]` is not divisible by :math:`q`,</span>
<span class="sd">        :math:`\\mathsf{high}` will be replaced with the maximum of :math:`k q + \\mathsf{low}</span>
<span class="sd">        \\lt \\mathsf{high}`, where :math:`k` is an integer.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        low:</span>
<span class="sd">            Lower endpoint of the range of the distribution. ``low`` is included in the range.</span>
<span class="sd">        high:</span>
<span class="sd">            Upper endpoint of the range of the distribution. ``high`` is included in the range.</span>
<span class="sd">        q:</span>
<span class="sd">            A discretization step.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">low</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">q</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">low</span> <span class="o">&gt;</span> <span class="n">high</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be smaller than or equal to the `high` value &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">, q=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">q</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">high</span> <span class="o">=</span> <span class="n">_adjust_discrete_uniform_high</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">q</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span>

<div class="viewcode-block" id="DiscreteUniformDistribution.single"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.DiscreteUniformDistribution.single">[文档]</a>    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">high</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">))</span>
        <span class="n">low</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">))</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">))</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">high</span> <span class="o">-</span> <span class="n">low</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">q</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span></div>

    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>

        <span class="n">value</span> <span class="o">=</span> <span class="n">param_value_in_internal_repr</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">value</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>


<div class="viewcode-block" id="IntUniformDistribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.IntUniformDistribution">[文档]</a><span class="k">class</span> <span class="nc">IntUniformDistribution</span><span class="p">(</span><span class="n">BaseDistribution</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A uniform distribution on integers.</span>

<span class="sd">    This object is instantiated by :func:`~optuna.trial.Trial.suggest_int`, and passed to</span>
<span class="sd">    :mod:`~optuna.samplers` in general.</span>

<span class="sd">    .. note::</span>
<span class="sd">        If the range :math:`[\\mathsf{low}, \\mathsf{high}]` is not divisible by</span>
<span class="sd">        :math:`\\mathsf{step}`, :math:`\\mathsf{high}` will be replaced with the maximum of</span>
<span class="sd">        :math:`k \\times \\mathsf{step} + \\mathsf{low} \\lt \\mathsf{high}`, where :math:`k` is</span>
<span class="sd">        an integer.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        low:</span>
<span class="sd">            Lower endpoint of the range of the distribution. ``low`` is included in the range.</span>
<span class="sd">        high:</span>
<span class="sd">            Upper endpoint of the range of the distribution. ``high`` is included in the range.</span>
<span class="sd">        step:</span>
<span class="sd">            A step for spacing between values.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">low</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">step</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">low</span> <span class="o">&gt;</span> <span class="n">high</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be smaller than or equal to the `high` value &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">step</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `step` value must be non-zero positive value, but step=</span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">high</span> <span class="o">=</span> <span class="n">_adjust_int_uniform_high</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">=</span> <span class="n">step</span>

    <span class="k">def</span> <span class="nf">to_external_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; int</span>

        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">param_value_in_internal_repr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_internal_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_external_repr</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; float</span>

        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">param_value_in_external_repr</span><span class="p">)</span>

<div class="viewcode-block" id="IntUniformDistribution.single"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.IntUniformDistribution.single">[文档]</a>    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span></div>

    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>

        <span class="n">value</span> <span class="o">=</span> <span class="n">param_value_in_internal_repr</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">value</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>


<div class="viewcode-block" id="IntLogUniformDistribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.IntLogUniformDistribution">[文档]</a><span class="k">class</span> <span class="nc">IntLogUniformDistribution</span><span class="p">(</span><span class="n">BaseDistribution</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A uniform distribution on integers in the log domain.</span>

<span class="sd">    This object is instantiated by :func:`~optuna.trial.Trial.suggest_int`, and passed to</span>
<span class="sd">    :mod:`~optuna.samplers` in general.</span>

<span class="sd">    .. note::</span>
<span class="sd">        If the range :math:`[\\mathsf{low}, \\mathsf{high}]` is not divisible by</span>
<span class="sd">        :math:`\\mathsf{step}`, :math:`\\mathsf{high}` will be replaced with the maximum of</span>
<span class="sd">        :math:`k \\times \\mathsf{step} + \\mathsf{low} \\lt \\mathsf{high}`, where :math:`k` is</span>
<span class="sd">        an integer.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        low:</span>
<span class="sd">            Lower endpoint of the range of the distribution. ``low`` is included in the range.</span>
<span class="sd">        high:</span>
<span class="sd">            Upper endpoint of the range of the distribution. ``high`` is included in the range.</span>
<span class="sd">        step:</span>
<span class="sd">            A step for spacing between values.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">low</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">step</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">low</span> <span class="o">&gt;</span> <span class="n">high</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be smaller than or equal to the `high` value &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">step</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `step` value must be non-zero positive value, but step=</span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">low</span> <span class="o">&lt;</span> <span class="mf">1.0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The `low` value must be equal to or greater than 1 for a log distribution &quot;</span>
                <span class="s2">&quot;(low=</span><span class="si">{}</span><span class="s2">, high=</span><span class="si">{}</span><span class="s2">).&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">high</span> <span class="o">=</span> <span class="n">_adjust_int_uniform_high</span><span class="p">(</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">step</span> <span class="o">=</span> <span class="n">step</span>

    <span class="k">def</span> <span class="nf">to_external_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; int</span>

        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">param_value_in_internal_repr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_internal_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_external_repr</span><span class="p">):</span>
        <span class="c1"># type: (int) -&gt; float</span>

        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">param_value_in_external_repr</span><span class="p">)</span>

<div class="viewcode-block" id="IntLogUniformDistribution.single"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.IntLogUniformDistribution.single">[文档]</a>    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">step</span></div>

    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>

        <span class="n">value</span> <span class="o">=</span> <span class="n">param_value_in_internal_repr</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">value</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span></div>


<div class="viewcode-block" id="CategoricalDistribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.CategoricalDistribution">[文档]</a><span class="k">class</span> <span class="nc">CategoricalDistribution</span><span class="p">(</span><span class="n">BaseDistribution</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A categorical distribution.</span>

<span class="sd">    This object is instantiated by :func:`~optuna.trial.Trial.suggest_categorical`, and</span>
<span class="sd">    passed to :mod:`~optuna.samplers` in general.</span>

<span class="sd">    Args:</span>
<span class="sd">        choices:</span>
<span class="sd">            Parameter value candidates.</span>

<span class="sd">    .. note::</span>

<span class="sd">        Not all types are guaranteed to be compatible with all storages. It is recommended to</span>
<span class="sd">        restrict the types of the choices to :obj:`None`, :class:`bool`, :class:`int`,</span>
<span class="sd">        :class:`float` and :class:`str`.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        choices:</span>
<span class="sd">            Parameter value candidates.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">choices</span><span class="p">):</span>
        <span class="c1"># type: (Sequence[CategoricalChoiceType]) -&gt; None</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">choices</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The `choices` must contains one or more elements.&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">choice</span> <span class="ow">in</span> <span class="n">choices</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">choice</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">choice</span><span class="p">,</span> <span class="p">(</span><span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">)):</span>
                <span class="n">message</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="s2">&quot;Choices for a categorical distribution should be a tuple of None, bool, &quot;</span>
                    <span class="s2">&quot;int, float and str for persistent storage but contains </span><span class="si">{}</span><span class="s2"> which is of type &quot;</span>
                    <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">choice</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="n">choices</span>

    <span class="k">def</span> <span class="nf">to_external_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; CategoricalChoiceType</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">param_value_in_internal_repr</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">to_internal_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_external_repr</span><span class="p">):</span>
        <span class="c1"># type: (CategoricalChoiceType) -&gt; float</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">param_value_in_external_repr</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;&#39;</span><span class="si">{}</span><span class="s2">&#39; not in </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">param_value_in_external_repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span>
            <span class="p">)</span> <span class="kn">from</span> <span class="nn">e</span>

<div class="viewcode-block" id="CategoricalDistribution.single"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.CategoricalDistribution.single">[文档]</a>    <span class="k">def</span> <span class="nf">single</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; bool</span>

        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span></div>

    <span class="k">def</span> <span class="nf">_contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">):</span>
        <span class="c1"># type: (float) -&gt; bool</span>

        <span class="n">index</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">param_value_in_internal_repr</span><span class="p">)</span>
        <span class="k">return</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span></div>


<span class="n">DISTRIBUTION_CLASSES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="n">UniformDistribution</span><span class="p">,</span>
    <span class="n">LogUniformDistribution</span><span class="p">,</span>
    <span class="n">DiscreteUniformDistribution</span><span class="p">,</span>
    <span class="n">IntUniformDistribution</span><span class="p">,</span>
    <span class="n">IntLogUniformDistribution</span><span class="p">,</span>
    <span class="n">CategoricalDistribution</span><span class="p">,</span>
<span class="p">)</span>


<div class="viewcode-block" id="json_to_distribution"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.json_to_distribution">[文档]</a><span class="k">def</span> <span class="nf">json_to_distribution</span><span class="p">(</span><span class="n">json_str</span><span class="p">):</span>
    <span class="c1"># type: (str) -&gt; BaseDistribution</span>
    <span class="sd">&quot;&quot;&quot;Deserialize a distribution in JSON format.</span>

<span class="sd">    Args:</span>
<span class="sd">        json_str: A JSON-serialized distribution.</span>

<span class="sd">    Returns:</span>
<span class="sd">        A deserialized distribution.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">json_dict</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">json_str</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">json_dict</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">CategoricalDistribution</span><span class="o">.</span><span class="vm">__name__</span><span class="p">:</span>
        <span class="n">json_dict</span><span class="p">[</span><span class="s2">&quot;attributes&quot;</span><span class="p">][</span><span class="s2">&quot;choices&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">json_dict</span><span class="p">[</span><span class="s2">&quot;attributes&quot;</span><span class="p">][</span><span class="s2">&quot;choices&quot;</span><span class="p">])</span>

    <span class="k">for</span> <span class="bp">cls</span> <span class="ow">in</span> <span class="n">DISTRIBUTION_CLASSES</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">json_dict</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="o">**</span><span class="n">json_dict</span><span class="p">[</span><span class="s2">&quot;attributes&quot;</span><span class="p">])</span>

    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown distribution class: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">json_dict</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]))</span></div>


<div class="viewcode-block" id="distribution_to_json"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.distribution_to_json">[文档]</a><span class="k">def</span> <span class="nf">distribution_to_json</span><span class="p">(</span><span class="n">dist</span><span class="p">):</span>
    <span class="c1"># type: (BaseDistribution) -&gt; str</span>
    <span class="sd">&quot;&quot;&quot;Serialize a distribution to JSON format.</span>

<span class="sd">    Args:</span>
<span class="sd">        dist: A distribution to be serialized.</span>

<span class="sd">    Returns:</span>
<span class="sd">        A JSON string of a given distribution.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">dist</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="s2">&quot;attributes&quot;</span><span class="p">:</span> <span class="n">dist</span><span class="o">.</span><span class="n">_asdict</span><span class="p">()})</span></div>


<div class="viewcode-block" id="check_distribution_compatibility"><a class="viewcode-back" href="../../reference/distributions.html#optuna.distributions.check_distribution_compatibility">[文档]</a><span class="k">def</span> <span class="nf">check_distribution_compatibility</span><span class="p">(</span><span class="n">dist_old</span><span class="p">,</span> <span class="n">dist_new</span><span class="p">):</span>
    <span class="c1"># type: (BaseDistribution, BaseDistribution) -&gt; None</span>
    <span class="sd">&quot;&quot;&quot;A function to check compatibility of two distributions.</span>

<span class="sd">    Note that this method is not supposed to be called by library users.</span>

<span class="sd">    Args:</span>
<span class="sd">        dist_old: A distribution previously recorded in storage.</span>
<span class="sd">        dist_new: A distribution newly added to storage.</span>

<span class="sd">    Returns:</span>
<span class="sd">        True denotes given distributions are compatible. Otherwise, they are not.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">dist_old</span><span class="o">.</span><span class="vm">__class__</span> <span class="o">!=</span> <span class="n">dist_new</span><span class="o">.</span><span class="vm">__class__</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot set different distribution kind to the same parameter name.&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist_old</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
        <span class="k">return</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dist_new</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
        <span class="k">return</span>
    <span class="k">if</span> <span class="n">dist_old</span><span class="o">.</span><span class="n">choices</span> <span class="o">!=</span> <span class="n">dist_new</span><span class="o">.</span><span class="n">choices</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="n">CategoricalDistribution</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">+</span> <span class="s2">&quot; does not support dynamic value space.&quot;</span>
        <span class="p">)</span></div>


<span class="k">def</span> <span class="nf">_adjust_discrete_uniform_high</span><span class="p">(</span><span class="n">low</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">q</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
    <span class="n">d_high</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">high</span><span class="p">))</span>
    <span class="n">d_low</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">low</span><span class="p">))</span>
    <span class="n">d_q</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">q</span><span class="p">))</span>

    <span class="n">d_r</span> <span class="o">=</span> <span class="n">d_high</span> <span class="o">-</span> <span class="n">d_low</span>

    <span class="k">if</span> <span class="n">d_r</span> <span class="o">%</span> <span class="n">d_q</span> <span class="o">!=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">&quot;0&quot;</span><span class="p">):</span>
        <span class="n">old_high</span> <span class="o">=</span> <span class="n">high</span>
        <span class="n">high</span> <span class="o">=</span> <span class="nb">float</span><span class="p">((</span><span class="n">d_r</span> <span class="o">//</span> <span class="n">d_q</span><span class="p">)</span> <span class="o">*</span> <span class="n">d_q</span> <span class="o">+</span> <span class="n">d_low</span><span class="p">)</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
            <span class="s2">&quot;The distribution is specified by [</span><span class="si">{low}</span><span class="s2">, </span><span class="si">{old_high}</span><span class="s2">] and q=</span><span class="si">{step}</span><span class="s2">, but the range &quot;</span>
            <span class="s2">&quot;is not divisible by `q`. It will be replaced by [</span><span class="si">{low}</span><span class="s2">, </span><span class="si">{high}</span><span class="s2">].&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">old_high</span><span class="o">=</span><span class="n">old_high</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">q</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">return</span> <span class="n">high</span>


<span class="k">def</span> <span class="nf">_adjust_int_uniform_high</span><span class="p">(</span><span class="n">low</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">step</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
    <span class="n">r</span> <span class="o">=</span> <span class="n">high</span> <span class="o">-</span> <span class="n">low</span>
    <span class="k">if</span> <span class="n">r</span> <span class="o">%</span> <span class="n">step</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">old_high</span> <span class="o">=</span> <span class="n">high</span>
        <span class="n">high</span> <span class="o">=</span> <span class="n">r</span> <span class="o">//</span> <span class="n">step</span> <span class="o">*</span> <span class="n">step</span> <span class="o">+</span> <span class="n">low</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
            <span class="s2">&quot;The distribution is specified by [</span><span class="si">{low}</span><span class="s2">, </span><span class="si">{old_high}</span><span class="s2">] and step=</span><span class="si">{step}</span><span class="s2">, but the range &quot;</span>
            <span class="s2">&quot;is not divisible by `step`. It will be replaced by [</span><span class="si">{low}</span><span class="s2">, </span><span class="si">{high}</span><span class="s2">].&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">old_high</span><span class="o">=</span><span class="n">old_high</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">step</span>
            <span class="p">)</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="n">high</span>


<span class="k">def</span> <span class="nf">_get_single_value</span><span class="p">(</span><span class="n">distribution</span><span class="p">):</span>
    <span class="c1"># type: (BaseDistribution) -&gt; Union[int, float, CategoricalChoiceType]</span>

    <span class="k">assert</span> <span class="n">distribution</span><span class="o">.</span><span class="n">single</span><span class="p">()</span>

    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span>
        <span class="n">distribution</span><span class="p">,</span>
        <span class="p">(</span>
            <span class="n">UniformDistribution</span><span class="p">,</span>
            <span class="n">LogUniformDistribution</span><span class="p">,</span>
            <span class="n">DiscreteUniformDistribution</span><span class="p">,</span>
            <span class="n">IntUniformDistribution</span><span class="p">,</span>
            <span class="n">IntLogUniformDistribution</span><span class="p">,</span>
        <span class="p">),</span>
    <span class="p">):</span>
        <span class="k">return</span> <span class="n">distribution</span><span class="o">.</span><span class="n">low</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">distribution</span><span class="o">.</span><span class="n">choices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">assert</span> <span class="kc">False</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>